package cn.ven.dao;

import cn.ven.entity.Blog;
import cn.ven.entity.BlogTypeContent;
import cn.ven.entity.Content;
import cn.ven.entity.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;


@Repository
public class BlogDaoImpl implements BlogDao {

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public void addBlog(Blog blog) {
        mongoTemplate.insert(blog);
    }

    @Override
    public void delBlog(String id) {
        Query query =new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Blog.class);
    }

    @Override
    public BlogTypeContent findBlogTypeContent(String id) {
        Blog blog = mongoTemplate.findById(id, Blog.class);
        Content content = mongoTemplate.findById(blog.getContentId(), Content.class);
        Type type = mongoTemplate.findById(blog.getTypeid(), Type.class);
        BlogTypeContent blogTypeContent =new BlogTypeContent();
        blogTypeContent.setBlog(blog);
        blogTypeContent.setContent(content);
        blogTypeContent.setType(type);
        return blogTypeContent;
    }

    @Override
    public List<Blog> findAll(int isdel) {
        Query query =new Query(Criteria.where("isdel").is(isdel));
        return mongoTemplate.find(query,Blog.class);
    }

    @Override
    public Blog findById(String id) {
        return mongoTemplate.findById(id,Blog.class);
    }

    @Override
    public List<Blog> findByUid(String uid) {
       Query query =new Query(Criteria.where("uid").is(uid));
        List<Blog> blogs = mongoTemplate.find(query, Blog.class);
        return blogs;
    }

    @Override
    public List<Blog> findByTypeid(String typeid) {
        Query query =new Query(Criteria.where("typeid").is(typeid));
        List<Blog> blogs = mongoTemplate.find(query, Blog.class);
        return blogs;
    }

    @Override
    public void updateBlog(Blog blog) {
        Blog blog1 = mongoTemplate.save(blog);
        System.out.println("blog1==="+blog1);
    }

    @Override
    public List<Blog> findBlogByTypeid(String typeid) {
        Query query =new Query(Criteria.where("typeid").is(typeid));
        return mongoTemplate.find(query,Blog.class);
    }


    @Override
    public List<Blog> findMyDelBlogs(String uid, int isdel) {
        Query query =new Query();
        query.addCriteria(Criteria.where("uid").is(uid).and("isdel").is(isdel));
        return mongoTemplate.find(query,Blog.class);
    }


}
